package com.nohgun.bike.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.nohgun.bike.R;

//import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
class MyOverlay extends MyLocationOverlay
{
	Location mMyBeforeLocation;
	Location mMyCurrentLocation;
	Path mPath;
	Paint mPaint;
	ArrayList<MyPathLocation> mMyPathLocationArray;
	Context mContext;
	MapView mMapView;
	float mMyDistance = 0;
	public MyOverlay(Context context, MapView mapView) 
	{
		super(context, mapView);
		
		mPath = new Path();
		mPath.reset();
		mPaint = new Paint();
    	mPaint.setAntiAlias(true);
    	mPaint.setDither(true);
    	mPaint.setColor(Color.RED);
    	mPaint.setStyle(Paint.Style.STROKE);
    	mPaint.setStrokeJoin(Paint.Join.ROUND);
    	mPaint.setStrokeCap(Paint.Cap.ROUND);
    	mPaint.setStrokeWidth(3);
    	mPaint.setTextSize(20);
    	mPaint.setStyle(Paint.Style.FILL);
    	mMyPathLocationArray = new ArrayList<MyPathLocation>();
    	
    	mContext = context;
    	mMapView = mapView;
	}
	
	public void onLocationChanged(Location location)
	{
		super.onLocationChanged(location);
		Log.d("###debug::MyLocationOverlay::onLocationChanged 01 ###","01");
		Double latitude = location.getLatitude();
		Double longitude = location.getLongitude();
		Geocoder gc = new Geocoder(mContext, Locale.getDefault());
		StringBuilder sb = new StringBuilder();
		try {
			List<Address> addresses = gc.getFromLocation(latitude, longitude, 10);
			
			if(addresses.size() > 0)
			{
				Address address = addresses.get(0);
				for(int i = 0 ; i <= address.getMaxAddressLineIndex() ; i++)
				{
					if(i > 0)
						sb.append(" ");
					sb.append(address.getAddressLine(i));
				}
			}
			else
			{
				sb.append(mContext.getString(R.string.unknown_address));
			}
			((MapTest)mContext).mMyLocationEditText.setText(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mMyBeforeLocation = mMyCurrentLocation;
		mMyCurrentLocation = location;
		if(mMyBeforeLocation != null && mMyCurrentLocation != null && mMyBeforeLocation != mMyCurrentLocation)
		{
			mMyPathLocationArray.add(new MyPathLocation(mMyBeforeLocation, mMyCurrentLocation));
			
			mMyDistance += mMyCurrentLocation.distanceTo(mMyBeforeLocation);
			
			((MapTest)mContext).mMyLocationEditText.setText(String.valueOf(mMyDistance));
		}
		else 
			return;
		
	}
	
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
	{
		Log.d("###debug::MyLocationOverlay::draw 02 ###","02");
		if(mMyBeforeLocation != null && mMyCurrentLocation != null)
		{
//			Point startPoint = new Point();
//			Point endPoint = new Point();
//			
//			mapView.getProjection().toPixels(new GeoPoint((int)(mMyBeforeLocation.getLatitude()*1E6), (int)(mMyBeforeLocation.getLongitude()*1E6)), startPoint);
//			mapView.getProjection().toPixels(new GeoPoint((int)(mMyCurrentLocation.getLatitude()*1E6), (int)(mMyCurrentLocation.getLongitude()*1E6)), endPoint);
//			
//			
			mPath.reset();
			canvas.drawPath(mPath, mPaint);
			updatePath();
//			mPath.moveTo(startPoint.x, startPoint.y);
//			mPath.lineTo(endPoint.x, endPoint.y);
			mPaint.setStyle(Paint.Style.STROKE);
			canvas.drawPath(mPath, mPaint);
			mPaint.setStyle(Paint.Style.FILL);
			canvas.drawTextOnPath(String.valueOf(mMyDistance), mPath, 0, 0, mPaint);
			/*
			MapController mc = mMapView.getController(); //실제좌표값을 맵뷰에 출력, 좌표값이 바뀌면 바뀐 위치를 화면에 출력
			MyPathLocation temp = mMyPathLocationArray.get(mMyPathLocationArray.size()-1);
            mc.animateTo(new GeoPoint((int)(temp.mMyCurrentLocation.getLatitude()*1E6), (int)(temp.mMyCurrentLocation.getLongitude()*1E6))); // animateTo() 메소드에 매개변수로 받는다. 이곳으로 지도의 중심으로 가게 한다.
			 
            mc.setZoom(16); //1~21 숫자가 클수록 정밀하게 표시 
            */
		}
		return super.draw(canvas, mapView, shadow, when);
	}
	
	public void updatePath()
	{
		//Log.d("###debug::MyLocationOverlay::updatePath 02 ###","02");
		for(int i = 0 ; i < mMyPathLocationArray.size() ; i++)
		{
			Point startPoint = new Point();
			Point endPoint = new Point();
			
			Point tempPoint = new Point();
			int check=0;
			
			MyPathLocation temp = mMyPathLocationArray.get(i);
			mMapView.getProjection().toPixels(new GeoPoint((int)(temp.mMyBeforeLocation.getLatitude()*1E6), (int)(temp.mMyBeforeLocation.getLongitude()*1E6)), startPoint);
			mMapView.getProjection().toPixels(new GeoPoint((int)(temp.mMyCurrentLocation.getLatitude()*1E6), (int)(temp.mMyCurrentLocation.getLongitude()*1E6)), endPoint);
			//Log.d("###debug::MyLocationOverlay::updatePath 03 ###",String.valueOf((int)(temp.mMyBeforeLocation.getLatitude()*1E6))+":::"+String.valueOf((int)(temp.mMyBeforeLocation.getLongitude()*1E6)));
			
			
			Path p = new Path();
			p.reset();
			
			/*
			p.moveTo(startPoint.x, startPoint.y);
			p.lineTo(endPoint.x, endPoint.y);
			mPath.addPath(p);
			*/
			
			if((Math.abs(startPoint.x - endPoint.x) > 15)||(Math.abs(startPoint.y - endPoint.y) > 15)){
				if(check==1){
					startPoint = tempPoint;
					check=0;
				}else{
					tempPoint = startPoint;
					check++;
				}
				continue;
			}else{
				p.moveTo(startPoint.x, startPoint.y);
				p.lineTo(endPoint.x, endPoint.y);
			}
			mPath.addPath(p);
			
			 
			
			
			/*
			 
                
                ZoomControls zoomControls = (ZoomControls) mMapView.getZoomControls();

			zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

			mMapView.addView(zoomControls);

			mMapView.displayZoomControls(true);
			  */
		}
	}
	
//	public boolean onTap(GeoPoint p, MapView map)
//	{
//		if(getLastFix() == null)
//			return false;
//		Double latitude = getLastFix().getLatitude();
//		Double longitude = getLastFix().getLongitude();
//		Geocoder gc = new Geocoder(mContext, Locale.getDefault());
//		StringBuilder sb = new StringBuilder();
//		try {
//			List<Address> addresses = gc.getFromLocation(latitude, longitude, 10);
//			
//			if(addresses.size() > 0)
//			{
//				Address address = addresses.get(0);
//				for(int i = 0 ; i <= address.getMaxAddressLineIndex() ; i++)
//				{
//					if(i > 0)
//						sb.append(" ");
//					sb.append(address.getAddressLine(i));
//				}
//			}
//			else
//			{
//				return false;
//			}
//			showToast(sb.toString());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return false;
//		}
//		return true;
//	}
}

class MyPathLocation
{
	public Location mMyBeforeLocation;
	public Location mMyCurrentLocation;
	
	public MyPathLocation(Location myBeforeLocation, Location myCurrentLocation)
	{
		mMyBeforeLocation = myBeforeLocation;
		mMyCurrentLocation = myCurrentLocation;
	}
}
